import { refreshLogin } from "../../api/login"
import { SET_ACCESS, UPDATE_ACCESS } from "../mutation-types"
export default {
    namespaced : true,
    state : {
        
    },

    actions : {
        async login({ commit, dispatch }, data) {
            commit(SET_ACCESS, data, {root : true})
            dispatch('deliveryRefreshTask')
        },

        deliveryRefreshTask({rootState, dispatch}) {
            setTimeout(() => {
                dispatch('refreshLogin')
            }, Math.floor(rootState.accessExpireIn * 1000 - ((new Date()).getTime())))
        },

        async refreshLogin({commit, rootState, dispatch}) {
            console.log('------------- 刷新登录------------')
            const res = await refreshLogin(rootState.refreshToken)
            if(res) {
                commit(UPDATE_ACCESS, res, {root : true})
                dispatch('deliveryRefreshTask')
            }
        }
    }
}